import 'package:flutter_test/flutter_test.dart';
import 'package:sow_app/repository/ajax_novel_repository.dart';
import 'package:sow_app/repository/ajax_rest_client.dart';
import 'package:sow_app_core/sow_app_core.dart';

void main() async {
  group('novel repository test group in one token', () {
    AjaxRestClient client = AjaxRestClient();
    client.setAuthorization(
        'eyJhbGciOiJIUzUxMiJ9.eyJleHAiOjE2NjE0MTg0NzcsInN1YiI6IntcImlkXCI6MTI1NTA2MDMyODMyMjAyNzUyMCxcInVzZXJuYW1lXCI6XCIxMzU2MDQyMTMyNFwiLFwibmlja05hbWVcIjpcIjEzNTYwNDIxMzI0XCJ9IiwiY3JlYXRlZCI6MTY2MDgxMzY3NzA1NX0.hszCyA4dyZDKCxcIkLg6eRwc08tSlMdAJBLP23pvz9mKfXw5rFzghc9UpnLDDRtUJ6IarZr0ZsaWe4o4fsMatg');
    test(
      "should return NovelModel when invoke novelDetail()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        var result = await repository.novelDetail("1262260513468559361");
        // assert
        expect(result, isNotNull);
        expect(result, isA<Novel>());
        expect(result.title, "阿斯");
      },
    );
    test(
      "should return PagedDataModel when invoke novelList()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        var result = await repository.novelList(null);
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
    test(
      "should return true when invoke subscribe()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        await repository.novelSubscribe("1");
        // assert
      },
    );
    test(
      "should return true when invoke unsubscribe()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        await repository.novelUnsubscribe("1");
        // assert
      },
    );
    test(
      "should return novelList when invoke getNovelListByClass()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getNovelListByClass("1");
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
    test(
      "should return novelList when invoke getEndBookRank()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getEndBookRank();
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
    test(
      "should return searchList when invoke getHotRank()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getHotRank();
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
    test(
      "should return chapter list when invoke getChapterList()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getChapterList("1262260513468559361");
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
    test(
      "should return chapter content when invoke getChapterContent()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getChapterContent(
            "1262260513468559361", "1557924663176384512");
        // assert
        expect(result, isNotNull);
      },
    );
    test(
      "should return ServerException exception when invoke novelRead() with wrong novelId",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        try {
          await repository.novelRead("-1");
        } on ServerException catch (e) {
          expect(e.msg, "文章不存在");
        }
        // assert
      },
    );
    test(
      "should return ServerException exception when invoke novelRead() with wrong novelId",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        await repository.novelRead("1");
        // assert
      },
    );
    test(
      "should return true exception when invoke novelIsSubscribe()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        await repository.novelIsSubscribe("1262260513468559361");
        // assert
      },
    );
    test(
      "should return not empty list  when invoke getBookShelf()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final bookShelf = await repository.getBookShelf();
        // assert
        expect(bookShelf, isNotEmpty);
      },
    );
    test(
      "should return not empty list  when invoke getReadHistory()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final readHistory = await repository.getReadHistory();
        // assert
        expect(readHistory, isNotEmpty);
      },
    );
    test(
      "should return not empty list  when invoke getClassifyList()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final classifyList = await repository.getHotClassifyList();
        // assert
        expect(classifyList, isNotEmpty);
      },
    );
    test(
      "should return not empty list  when invoke getClassifyList()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final classifyList = await repository.getHotClassifyList();
        // assert
        expect(classifyList, isNotEmpty);
      },
    );
    test(
      "should return novelList when invoke getRecommendRank()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getRecommendRank();
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
    test(
      "should return novelList when invoke getHighScoreRank()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getHighScoreRank();
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
    test(
      "should return novelList when invoke getGuessYouLikeRank()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getGuessYouLikeRank();
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
    test(
      "should return novelList when invoke getHotUpRank()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getHotUpRank();
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
    test(
      "should return novelList when invoke getNewComeRank()",
      () async {
        // arrange
        NovelRepository repository = AjaxNovelRepository(client);
        // act
        final result = await repository.getNewComeRank();
        // assert
        expect(result, isA<List>());
        expect(result.length, greaterThan(0));
      },
    );
  });
}
